package com.ws.game.lesson1.keycontrol
{
	import com.ws.game.common.framework.BaseCtrl;
	import com.ws.game.common.framework.BaseGameObject;
	import com.ws.game.common.utils.MoveDirectionType;
	import com.ws.game.common.global.Global;
	import com.ws.log.WSLog;
	
	public class ActionObject extends BaseGameObject
	{
		protected var speed:Number = 2;
		
		protected var walkDirection:uint=MoveDirectionType.STOP;
		protected var ctrl:BaseCtrl;		
		
		public function ActionObject(aCtrl:BaseCtrl)
		{
			super();
			this.ctrl = aCtrl;
			this.ctrl.target = this;
		}
		
		public function set direction(dir:uint):void
		{
			walkDirection = dir;
		}
		
		protected function move():void
		{			
			if (!nextCanMove) {
				return; 
			}
			switch(walkDirection)
			{
				case MoveDirectionType.UP:
					y -= speed;
					break;
				case MoveDirectionType.DOWN:
					y += speed;
					break;
				case MoveDirectionType.LEFT:
					x -= speed;
					break;
				case MoveDirectionType.RIGHT:
					x += speed;
					break;
				default:
					break;
			}
			WSLog.info("({0},{1})",x,y);
		}
		
		public function get nextCanMove():Boolean
		{
			var nx:uint = 0;
			var ny:uint = 0;
			// 根据移动方向进行处理，计算出下一目标点位置
			switch(walkDirection)
			{
				case MoveDirectionType.UP:
					ny = y-speed;
					break;
				case MoveDirectionType.DOWN:
					ny = y+speed;
					break;
				case MoveDirectionType.LEFT:
					nx = x-speed;
					break;
				case MoveDirectionType.RIGHT:
					nx = x+speed;
					break;
				default:break;
			}
			
			// 如果下一目标点超出屏幕范围，则不能移动
			if (nx > Global.stage.stageWidth - width || nx < 0) 
				return false;
			if (ny > Global.stage.stageHeight - height || ny < 0)
				return false;
			
			// 检测通过
			return true;
		}
		
		override public function doWork():void
		{
			if (walkDirection != MoveDirectionType.STOP) 
					move();
			super.doWork();
		}
		
		override public function dispose():void{
			this.ctrl.dispose();
		}
	}
}